package com.daoliuhe.scaffold.tools;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd--HH:mm:ss");

    public static String nowString(String pattern) {
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(new Date());
    }

    /**
     * 当前的日期格式
     * yyyy-MM-dd
     *
     * @return
     */
    public static String nowSimpleString() {
        return sdf2.format(new Date());
    }

    /**
     * 和当前偏移了的日期
     *
     * @param year  年
     * @param month 月
     * @param day   日
     * @return yyyy-MM-dd 格式字符串
     */
    public static String daySimpleString(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.YEAR, year);
        cal.add(Calendar.MONTH, month);
        cal.add(Calendar.DAY_OF_YEAR, day);
        return sdf2.format(cal.getTime());
    }

    /**
     * 格式化
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        if (date != null) {
            return sdf.format(date);
        }
        return "";
    }

    /**
     * 格式化
     *
     * @param date
     * @return
     */
    public static String format2(Date date) {
        if (date != null) {
            return sdf2.format(date);
        }
        return "";
    }

    /**
     * 格式化
     *
     * @param date
     * @return
     */
    public static String format3(Date date) {
        if (date != null) {
            return sdf3.format(date);
        }
        return "";
    }

    /**
     * 解析日期字符串
     *
     * @param dateStr
     * @return
     */
    public static Date parseDateStr(String dateStr) {
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 解析日期字符串
     *
     * @param dateStr
     * @return
     */
    public static Date parseDateStr2(String dateStr) {
        Date date = null;
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf1.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
